package com.transpot.controller.money;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.transpot.entry.money.Reimbursement;
import com.transpot.entry.money.Stream;
import com.transpot.entry.order.OrderReturn;
import com.transpot.entry.order.OrderStatus;
import com.transpot.service.money.ReimbursementService;
import com.transpot.service.money.StreamService;
import com.transpot.utils.R;
import com.transpot.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/reimbursement")
public class ReimbursementController {
    @Autowired
    private ReimbursementService reimbursementService;
    @Autowired
    private StreamService streamService;
    /**
     * 申请报销功能
     * @param multipartFile
     * @param Json
     * @return
     */
    @PostMapping("/addReimbursement")
    public R addReimbursement(@RequestParam("file") MultipartFile multipartFile, @RequestParam("form") String Json) {
        //调用自定义的图片保存方法
        String url = UploadUtil.uploadImg(multipartFile);
        //创建json转化对象
        ObjectMapper mapper = new ObjectMapper();
        //因为java8的日期类型jackson不能转化因此要加一个时间模块
        mapper.registerModule(new JavaTimeModule());
        //把json字符串转成对象
        Reimbursement reimbursement = null;
        try {
            reimbursement = mapper.readValue(Json, Reimbursement.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //设置图片访问的url
        reimbursement.setUrl(url);
        //设置状态为未处理
        reimbursement.setStatus(0);
        //设置创建时间为当前
        reimbursement.setCreateTime(LocalDateTime.now());
        //调用mp的方法保存报销申请
        reimbursementService.save(reimbursement);
        return R.success();
    }

    /**
     * 获取全部未处理的报销
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getReimbursement")
    public R getReimbursement(int current, int size) {
        //创建报销单分页查询对象
        Page<Reimbursement> reimbursementPage = new Page<>(current, size);
        //创建报销单条件查询对象
        LambdaQueryWrapper<Reimbursement> reimbursementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件为未处理的报销单
        reimbursementLambdaQueryWrapper.eq(Reimbursement::getStatus, 0);
        //执行分页查询
        reimbursementService.page(reimbursementPage, reimbursementLambdaQueryWrapper);
        //返回
        return R.success(reimbursementPage);
    }

    /**
     * 通过报销方法
     * @param reimbursement
     * @return
     */
    @PostMapping("/finish")
    public R finishReimbursement(@RequestBody Reimbursement reimbursement) {
        //更改状态为通过
        reimbursement.setStatus(1);
        //更改
        reimbursementService.updateById(reimbursement);
        //把报销添加到支出中
        Stream stream = new Stream();
        stream.setMoney(reimbursement.getNumber());
        //流水类型为支出
        stream.setType(1);
        //设置说明
        stream.setMark(reimbursement.getWay() + "报销支出");
        //设置创建时间
        stream.setCreateTime(LocalDateTime.now());
        //设置订单主键
        stream.setBaseId(reimbursement.getBaseId());
        //信息插入数据库
        streamService.save(stream);
        //返回
        return R.success();
    }

    /**
     * 拒绝报销理由方法
     * @param reimbursement
     * @return
     */
    @PostMapping("/question")
    public R updateReimbursement(@RequestBody Reimbursement reimbursement) {
        //设置报销状态为已经拒绝
        reimbursement.setStatus(2);
        reimbursementService.updateById(reimbursement);
        return R.success();
    }

    /**
     * 获取司机的全部报销单
     * @param current
     * @param size
     * @param id
     * @return
     */
    @GetMapping("/getReimbursementById")
    public R getReimbursementById(int current, int size, Integer id) {
        //创建报销单分页查询对象
        Page<Reimbursement> reimbursementPage = new Page<>(current, size);
        //创建条件查询对象
        LambdaQueryWrapper<Reimbursement> reimbursementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置条件为与该司机主键一致,并且按时间排序
        reimbursementLambdaQueryWrapper.eq(Reimbursement::getDriverId, id).orderByDesc(Reimbursement::getCreateTime);
        //执行查询
        reimbursementService.page(reimbursementPage, reimbursementLambdaQueryWrapper);
        return R.success(reimbursementPage);
    }

    /**
     * 删除报销单
     * @param reimbursement
     * @return
     */
    @PostMapping("/delReimbursement")
    public R delReimbursement(@RequestBody Reimbursement reimbursement) {
        //直接调用mp方法删除
        reimbursementService.removeById(reimbursement);
        return R.success();
    }
}
